package com.beaven.model.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtil {
  private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd", Locale.ENGLISH);

  /**
   * 获取一个简单的时间字符串
   *
   * @param date Date
   * @return 时间字符串
   */
  public static String getSampleDate(Date date) {
    return FORMAT.format(date);
  }

  /**
   * 根据时间获取界面显示状态
   * 当天：HH:mm
   * 昨天: 昨天
   * 之前: MM:dd
   *
   * @param date 时间
   * @return 状态值
   */
  public static String getDateStatus(Date date) {
    long time = date.getTime();
    long yesterdayStart = getYesterdayStartTimeStamp();
    if (yesterdayStart == 0) {
      return getSampleDate(date);
    }
    long todayStart = yesterdayStart + 24 * 60 * 60 * 1000;
    if (time < yesterdayStart) {
      return getSampleDate(date);
    } else if (time >= yesterdayStart && time < todayStart) {
      return "昨天";
    } else {
      SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
      return format.format(date);
    }
  }

  /**
   * 昨天00:00:00的unix时间戳
   */
  public static long getYesterdayStartTimeStamp() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    String yesterday = format.format(calendar.getTime()) + " 00:00:00";
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    try {
      Date date = sdf.parse(yesterday);
      return date.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return 0;
  }
}
